import time
from datetime import datetime

from selenium.webdriver import ActionChains
from selenium.webdriver.support.select import Select

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

# 创建浏览器驱动对象
driver= webdriver.Edge()
driver.maximize_window()
driver.implicitly_wait(5)
#打开页面
driver.get("http://localhost:82/index.php")

driver.find_element(By.CSS_SELECTOR,'.red').click()
driver.find_element(By.CSS_SELECTOR,'#username').send_keys("13800138006")
driver.find_element(By.CSS_SELECTOR,'#password').send_keys("123456")
driver.find_element(By.CSS_SELECTOR,'#verify_code').send_keys("8888")
driver.find_element(By.CSS_SELECTOR,"a[name='sbtbutton']").click()

time.sleep(1)
driver.find_element(By.CSS_SELECTOR,"a[href='/index.php/Home/Index/index.html']").click()

# 搜索


driver.find_element(By.CSS_SELECTOR,'#q').send_keys("手机")

# 点击搜索
driver.find_element(By.CSS_SELECTOR,"button[type='submit']").click()
time.sleep(1)
# 点击第一个商品
driver.find_element(By.CSS_SELECTOR,"body > div:nth-child(7) > div:nth-child(1) > div:nth-child(2) > div:nth-child(2) > ul:nth-child(1) > li:nth-child(1) > div:nth-child(1) > div:nth-child(1) > a:nth-child(1)").click()
#goods_spec_a_13
driver.find_element(By.CSS_SELECTOR,"#goods_spec_a_13").click()
driver.find_element(By.CSS_SELECTOR,"#goods_spec_a_17").click()

# time.sleep(3)
# driver.find_element(By.CSS_SELECTOR,"#number").clear()  #清除   全选
time.sleep(3)
driver.find_element(By.CSS_SELECTOR,"#number").send_keys("2")  #清除
time.sleep(3)
driver.find_element(By.CSS_SELECTOR,"#buy_now").click()
time.sleep(2)

# 弹出框
driver.switch_to.alert.accept()

driver.find_element(By.CSS_SELECTOR,"#addNewAddress").click()
# 弹出新增收货地址


user_name=f"cur{datetime.now().strftime('%H%M%S')}"

driver.find_element(By.CSS_SELECTOR,"input[name='consignee'][type='text']").send_keys(user_name)


# 表单


driver.find_element(By.CSS_SELECTOR,"input[placeholder='固话号码格式:xx-xx(例如: 0755-86140485)']").send_keys("13412122323")

Select(driver.find_element(By.CSS_SELECTOR,"#province")).select_by_visible_text("山西")

Select(driver.find_element(By.CSS_SELECTOR,"#city")).select_by_visible_text("太原市")
Select(driver.find_element(By.CSS_SELECTOR,"#district")).select_by_visible_text("市辖区")

# 详细地址
driver.find_element(By.CSS_SELECTOR,"input[placeholder='详细地址']").send_keys("哈哈哈")
# 邮编
driver.find_element(By.CSS_SELECTOR,"input[name='zipcode']").send_keys("123456")
time.sleep(3)
# 点击保存
ele=driver.find_element(By.ID,"address_submit")
ele=WebDriverWait(driver,5).until(lambda  x:x.find_element(By.ID,"address_submit"))
# ele.click()
ActionChains(driver).click(ele).perform()

#ajax_address > div.consignee-list.p > ul > li:nth-child(5) > div.item_select_t.fl
time.sleep(2)
# 点击更多地址

ActionChains(driver).click(driver.find_element(By.CSS_SELECTOR,"div[class='addr-switch'] span")).perform()
time.sleep(3)
#
# # 选择地址
driver.find_element(By.CSS_SELECTOR,"#ajax_address > div.consignee-list.p > ul > li:nth-child(5) > div.item_select_t.fl").click()
# 勾选使用账户余额

driver.find_element(By.CSS_SELECTOR,"#user_money_checkbox").click()

# 获取商品的总金额

goods_money_ele=driver.find_element(By.CSS_SELECTOR,"#payables")
goods_money=goods_money_ele.text[1:]

time.sleep(4)
# 定位余额的输入框

driver.find_element(By.CSS_SELECTOR,"#user_money").send_keys(goods_money)

# 在定位支付密码

driver.find_element(By.CSS_SELECTOR,"#pay_pwd").send_keys("123456")

# 点击提交订单

driver.find_element(By.CSS_SELECTOR,"#submit_order").click()


# 打印订单编号
ele_order_no=driver.find_element(By.CSS_SELECTOR,"div[class='home-main'] span:nth-child(2)")
print(ele_order_no.text)










time.sleep(10)


#关闭浏览器驱动对象
driver.quit()
